// pages/house/RentalHouseDetail/addAudio/addAudio.js
const computedBehavior = require('miniprogram-computed')
import p from '../../../../utils/platform'
import Audio from '../../../../utils/audio'
import api from '../../../../common/api'
import { IMG_COMPOUND_VIDEO } from '../../../../common/menuId'

const app = getApp()
let audio = null
Page({
  behaviors: [computedBehavior],

  /**
   * 页面的初始数据
   */
  data: {
    placeholder: "",
    value: "",
    maxCount: 500,
    voiceType: "0",
    items: [
      { value: '0', name: '男声', avatar: '../images/boy.png', iconState: false, audioSrc: '/pages/house/RentalHouseDetail/images/2.mp3' },
      { value: '1', name: '女声', avatar: '../images/girl.png', iconState: false, audioSrc: '/pages/house/RentalHouseDetail/images/1.mp3' },
      { value: '2', name: '放弃视频配音' },
    ],
    delId: "",
    delType: "",
    required: '0',
    sceneIdList: "",
  },

  computed: {
    countText(data) {
      return (data.maxCount - data.value.length) + '/' + data.maxCount
    }
  },

  /**
   * 生命周期函数--监听页面加载
   * required 1则为配音，即不可选择放弃配音
   */
  onLoad: function(options) {
    console.log("配音页面参数", options);

    app.globalData.loginPromise.then(() => {
      if (options.required == '1') {
        this.setData({
          items: this.data.items.slice(0, 2)
        })
      }

      this.setData({
        delId: options.delId,
        delType: options.delType,
        required: options.required,
        sceneIdList: options.sceneIdList,
      })

      api.getDubbingInfo({
        delId: options.delId,
        delType: options.delType,
      }, ({ data }) => {
        if (data.isSuccess) {
          this.setData({
            value: data.content.slice(0, this.data.maxCount)
          })
        }
      })
    })
  },

  handlePlay(e) {
    if (audio && audio.isPlaying) {
      return p.showToast({
        title: "正在播放音频，请稍后",
        icon: "none",
      })
    }

    let value = e.currentTarget.dataset.value
    let state = this.data.items[value].iconState
    this.setData({
      [`items[${value}].iconState`]: !state
    })

    if (!state) {
      audio = audio || new Audio()
      audio.play(this.data.items[value].audioSrc)
        .then(() => {
          this.setData({
            [`items[${value}].iconState`]: false
          })
        })
    }
  },

  handleInput(e) {
    const value = e.detail.value
    this.setData({
      value: value.slice(0, this.data.maxCount)
    })
  },

  radioChange(e) {
    this.setData({
      voiceType: e.detail.value
    })
  },
  handleConfirm() {
    let that = this;
    api.saveDubbingInfo({
      delId: this.data.delId,
      dubbingInfo: this.data.value
    }, ({ data }) => {
      if (!data.isSuccess) return
      let obj = {
        delId: this.data.delId,
        taskCode: this.data.required == '1' ? 'videodubbing' : 'spheretovideo',
      }

      // 合成配音
      if (this.data.required == '1') {
        obj.dubbed = this.data.voiceType

        // 合成视频
      } else {
        obj.sceneIdList = this.data.sceneIdList.split(',')
        if (this.data.voiceType != '2') {
          obj.dubbed = this.data.voiceType
        }
      }

      api.submitSphereToVideoTask(obj, ({ data }) => {
        if (data.isSuccess) {
          const text = this.data.required=='1' ? '配音任务已提交，大约需要2到3分钟，请稍后到房源详情查看' : '视频合成任务已提交，大概需1小时，在房源列表页可查看进度'
          p.showModal({
            content: text,
            showCancel: false,
            confirmText: "我知道了"
          }).then(() => {
            if(that.data.required != 1) {
              api.getMemberPower({menuId: IMG_COMPOUND_VIDEO},(res) => {
                if(res.data.isSuccess){
                  wx.setStorageSync('memberInfo', res.data.content.rows)
                }
              })
            }
            wx.navigateBack({
              delta: this.data.required=='1' ? 1 : 2
            })
          })
        }
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {





  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    if (audio) {
      audio.destroy()
      audio = null
    }
    var that = this;
    var pages = getCurrentPages();
    console.log(222,pages)
    var prevPage = pages[pages.length - 3]; //上一个页面
    if (prevPage && prevPage.setData) {
      prevPage.setData({
        isUpdate: true,
        memberOption: false,
        showMoreOptions: false,
      })
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
})